package com.mall.huohuo.common.security;


import com.mall.huohuo.common.constant.UserConstants;
import com.mall.huohuo.common.domain.HuohuoUser;
import com.mall.huohuo.common.enums.TokenTypeEnum;
import com.mall.huohuo.common.jwt.JwtUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.lang.Assert;

import java.util.LinkedHashMap;
import java.util.Map;


/**
 * 安全工具类
 */
public class SecurityUtils {

    private static final String TOEKN_TYPE = "tokenType";

    /**
     * 创建用户accessToken
     *
     * @param user 用户
     * @return accessToken
     */
    public static String createUserAccessToken(HuohuoUser user) {
        return createUserAccessToken(user, UserConstants.USER_ACCESS_TOKEN_EXPIRE_SECONDS);
    }




    /**
     * 创建用户accessToken
     *
     * @param user 用户
     * @return accessToken
     */
    public static String createUserAccessToken(HuohuoUser user, long expireSeconds) {
        Assert.notNull(user, "要生成token的用户不能为空。");
        Map<String, Object> claims = new LinkedHashMap<>();
        claims.put("userId", user.getId());
        claims.put("userName", user.getName());
        // claims.put("admin", user.getAdmin()==null?false:user.getAdmin());
        claims.put(TOEKN_TYPE, TokenTypeEnum.ACCESS_TOKEN.getValue());

        return JwtUtils.createToken(claims,expireSeconds);
    }

    /**
     * 创建refreshToken
     *
     * @param user 用户
     * @return refreshToken
     */
    public static String createUserRefreshToken(HuohuoUser user) {
        Assert.notNull(user, "要生成token的用户不能为空。");

        Map<String, Object> claims = new LinkedHashMap<>();
        claims.put("userId", user.getId());
        claims.put("userName", user.getName());
      //  claims.put("admin", user.getAdmin()==null?false:user.getAdmin());
        claims.put(TOEKN_TYPE, TokenTypeEnum.REFRESH_TOKEN.getValue());

        return JwtUtils.createToken(claims, UserConstants.USER_REFRESH_TOKEN_EXPIRE_SECONDS);
    }

    /**
     * 根据accessToken获取用户
     *
     * @param accessToken accessToken
     * @return 用户
     */
    public static HuohuoUser getUserByAccessToken(String accessToken) {
        Assert.hasText(accessToken, "要获取用户的accessToken不能为空。");

        Claims claims = JwtUtils.getClaims(accessToken);
        Object tokenTypeObj = claims.get(TOEKN_TYPE);
        if (tokenTypeObj == null) {
            throw new IllegalArgumentException("accessToken 不合法。");
        }
        int tokenType = (int) tokenTypeObj;
        if (tokenType != TokenTypeEnum.ACCESS_TOKEN.getValue()) {
            throw new IllegalArgumentException("tokenType 不合法。");
        }

        HuohuoUser huohuoUser = new HuohuoUser();
        huohuoUser.setId(Long.parseLong(String.valueOf(claims.get("userId"))))
                .setName(String.valueOf(claims.get("userName")));
               // .setAdmin((boolean)claims.get("admin"));

        return huohuoUser;
    }

    /**
     * 根据refreshToken获取用户id
     *
     * @param refreshToken refreshToken
     * @return 用户
     */
    public static HuohuoUser getUserIdByRefreshToken(String refreshToken) {
        Assert.hasText(refreshToken, "要获取用户id的refreshToken不能为空。");

        Claims claims = JwtUtils.getClaims(refreshToken);
        Object tokenTypeObj = claims.get(TOEKN_TYPE);
        if (tokenTypeObj == null) {
            throw new IllegalArgumentException("refreshToken 不合法。");
        }
        int tokenType = (int) tokenTypeObj;
        if (tokenType != TokenTypeEnum.REFRESH_TOKEN.getValue()) {
            throw new IllegalArgumentException("refreshToken 不合法。");
        }


        HuohuoUser huohuoUser = new HuohuoUser();
        huohuoUser.setId((Long) claims.get("userId"))
                .setName(String.valueOf(claims.get("userName")));
        // .setAdmin((boolean)claims.get("admin"));

        return huohuoUser;
    }

}
